package com.xiyu.service.repository.medicine;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.medicine.*;
import com.xiyu.service.vo.health.medicineDeposit.MedicineDepositPageQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.List;

public interface HealthMedicineDepositRepository extends JRepository<HealthMedicineDeposit, Long> {
    HealthMedicineDepositTable healthMedicineDepositTable = HealthMedicineDepositTable.$;

    default Page<HealthMedicineDeposit> pageQuery(MedicineDepositPageQueryInput inputVO){
        return sql().createQuery(healthMedicineDepositTable)
                .whereIf(inputVO.getMedicineDepositType() != null, healthMedicineDepositTable.medicineDepositType().eq(inputVO.getMedicineDepositType()))
                .whereIf(StringUtils.hasText(inputVO.getElderName()), () -> healthMedicineDepositTable.elder().name().like(inputVO.getElderName()))
                .whereIf(StringUtils.hasText(inputVO.getMedicineName()), () -> healthMedicineDepositTable.medicine().medicineName().like(inputVO.getMedicineName()))
                .orderBy(healthMedicineDepositTable.id().desc())
                .select(healthMedicineDepositTable.fetch(HealthMedicineDepositFetcher.$.allTableFields()
                        .elder(ElderInfoFetcher.$.allTableFields())
                        .medicine(HealthMedicineFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    List<HealthMedicineDeposit> findByElderId(Long elderId);


}